<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew ref="searchuinew" :searchFormArr="searchFormArr" @searchParma="searchParma" @change="searchChange" :isAccording="isAccording"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="园区名称" prop="park_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系电话" prop="contact_info"></el-table-column>
          <el-table-column align="center" header-align="center" label="地址" prop="park_address"></el-table-column>
          <el-table-column align="center" header-align="center" label="经营品类" prop="business_category" width="200"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDetail(scope)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
      <!-- 弹窗-->
      <popup ref="popup"></popup>
    </div>
  </div>
</template>

<script>
import { comBaseApi, comCityApi, apiAdArr } from '@/api/customer'
import { formatFormData } from '@/utils'
import popup from './popup'
export default {
  name: 'index',
  components: { popup },
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '名称：',
          type: 'input',
          field: 'name',
          placeholder: '请输入名称',
        },
        {
          fieldName: '省：',
          type: 'select',
          field: 'sheng',
          option: [],
          placeholder: '请选择地区',
        },
        {
          fieldName: '市：',
          type: 'select',
          field: 'shi',
          option: [],
          placeholder: '请选择地区',
        },
        {
          fieldName: '主体类型：',
          type: 'select',
          field: 'subject_loc_type',
          option: [
            { id: '市场', text: '市场' },
            { id: '开发区', text: '开发区' },
            { id: '园区', text: '园区' },
          ],
          placeholder: '请选择主体类型',
        },
        {
          fieldName: '经营品类：',
          type: 'select',
          field: 'business_category',
          option: [
            { id: '塑料', text: '塑料' },
            { id: '废金属', text: '废金属' },
            { id: '二手设备', text: '二手设备' },
          ],
          placeholder: '请选择经营品类',
        },
      ],
      isAccording: true,
      searchParams: {
        name: '',
        sheng: '',
        shi: '',
        subject_loc_type: '',
        business_category: '',
      },
      shengList: [],
      shiList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
    }
  },
  mounted() {
    this.getDataList()
    this.getSelect()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams)
      apiAdArr(formatFormData(info))
        .then((res) => {
          const { items, total } = res.result

          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 获取下拉信息
    getSelect() {
      comBaseApi()
        .then((res) => {
          this.shengList = res.data.sheng
          this.setShengOption()
        })
        .catch((e) => {
          this.shengList = []
        })
    },
    setShengOption() {
      const item = this.searchFormArr.find((item) => {
        return item.field === 'sheng'
      })
      item.option = this.shengList.map((item) => {
        return { id: item, text: item }
      })
    },
    searchChange({ item, fieldArr }) {
      if (item.field === 'sheng') {
        this.initAreaCities(fieldArr.sheng)
        this.$refs.searchuinew.clearFormItem('shi')
        this.searchParams.shi = ''
      }
    },
    // 获取市级数据
    initAreaCities(sheng) {
      if (sheng && sheng !== '不限') {
        const formData = new FormData()
        formData.set('name', sheng)
        comCityApi(formData)
          .then((res) => {
            this.shiList = res.data.city_arr
            this.setShiOption()
          })
          .catch((e) => {
            this.shiList = []
            this.setShiOption()
          })
      } else {
        this.shiList = []
        this.setShiOption()
      }
    },
    setShiOption() {
      const item = this.searchFormArr.find((item) => {
        return item.field === 'shi'
      })
      item.option = this.shiList.map((item) => {
        return { id: item, text: item }
      })
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 查看详情
    handleDetail(scope) {
      this.$refs.popup.open(scope.row)
    },
  },
}
</script>

<style scoped lang="scss"></style>
